package satir.testcase.command;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.NoSuchElementException;

import org.junit.Test;

import satir.SatirGame;
import satir.command.Command;
import satir.command.EnterPlayerNumCmd;
import satir.framework.test.PrivateAccessor;
import junit.framework.TestCase;

public class EnterPlayerNumCmd_excute_1 extends TestCase {
	private EnterPlayerNumCmd command;
	private SatirGame mockSatirGame;

	/**
	 * Sets up the test fixture.
	 * 
	 * Called before every test case method.
	 */
	public void setUp() {
		mockSatirGame = new SatirGame();
		command = new EnterPlayerNumCmd(mockSatirGame);
	}

	/**
	 * Tears down the test fixture.
	 * 
	 * Called after every test case method.
	 */
	public void tearDown() {
	}

	/**
	 * Step number : 1
	 * Goal: numberOfPlayer = 2
	 */
	@Test
	public void test2Player() {
		int numberOfPlayer = 2;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		command.execute();
		try {
			int actualResult = ((Integer) PrivateAccessor.getPrivateField(
					mockSatirGame, "numOfPlayer")).intValue();
			assertEquals(2, actualResult);
		} catch (Exception e) {
			fail("Suberting JAVA Access Protection Failure.");
		}
	}
	
	/**
	 * Step number : 2
	 * Goal: numberOfPlayer = 5
	 */
	@Test
	public void test5Player() {
		int numberOfPlayer = 5;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		command.execute();
		try {
			int actualResult = ((Integer) PrivateAccessor.getPrivateField(
					mockSatirGame, "numOfPlayer")).intValue();
			assertEquals(5, actualResult);
		} catch (Exception e) {
			fail("Suberting JAVA Access Protection Failure.");
		}
	}
	
	/**
	 * Step number : 3
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void test1Player() {
		int numberOfPlayer = 1;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
			
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
	
	/**
	 * Step number : 4
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void test7Player() {
		int numberOfPlayer = 7;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {

		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
	
	/**
	 * Step number : 5
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void test123Player() {
		int numberOfPlayer = 123;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
			
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
	
	/**
	 * Step number : 6
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void test222Player() {
		int numberOfPlayer = 222;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	
	/**
	 * Step number : 7
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testAPlayer() {
		String input = "A";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
			
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
	
	/**
	 * Step number : 8
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testSPlayer() {
		String input = "!";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
			
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
}